package org.exez.modules.visitor.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.exez.common.api.vo.Result;
import org.exez.common.system.query.QueryGenerator;
import org.exez.common.aspect.annotation.AutoLog;
import org.exez.common.system.vo.LoginUser;
import org.exez.common.util.oConvertUtils;
import org.exez.modules.visitor.constant.AuthorityEnum;
import org.exez.modules.visitor.entity.VisitorAccessre;
import org.exez.modules.visitor.entity.XdPeople;
import org.exez.modules.visitor.service.IXdPeopleService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.exez.common.system.base.controller.JeecgController;
import org.exez.modules.visitor.util.AES;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 西店数据
 * @Author: exez-boot
 * @Date:   2020-08-11
 * @Version: V1.0
 */
@Slf4j
@Api(tags="西店数据")
@RestController
@RequestMapping("/visitor/xdPeople")
public class XdPeopleController extends JeecgController<XdPeople, IXdPeopleService> {
	@Autowired
	private IXdPeopleService xdPeopleService;


        /**
         * 西店重点人员数据-分页列表查询
         *
         * @param pageNo
         * @param pageSize
         * @return
         */
        @AutoLog(value = "西店重点人员数据-分页列表查询")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "keyWord", value = "关键字", required = false, dataType = "String", paramType = "query"),
                @ApiImplicitParam(name = "isKey", value = "对比结果 1是  0否", required = false, dataType = "Integer", paramType = "query"),
                @ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "Integer", paramType = "query"),
                @ApiImplicitParam(name = "pageSize", value = "页码大小", required = true, dataType = "Integer", paramType = "query"),
        })
        @ApiOperation(value = "西店重点人员--列表", notes = "西店重点人员--列表")
        @GetMapping(value = "/xdPeoplelist")
        public Result<IPage<XdPeople>> xdPeoplelist(
                @RequestParam(name = "keyWord", required = false) String keyWord,
                @RequestParam(name = "isKey", required = false) Integer isKey,
                @RequestParam(name = "pageNo" ,defaultValue="1") Integer pageNo,
                @RequestParam(name = "pageSize",defaultValue="10") Integer pageSize){


	    Result<IPage<XdPeople>> result = new Result<>();
		Page<XdPeople> page = new Page<>(pageNo, pageSize);

		LambdaQueryWrapper<XdPeople> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(XdPeople::getCreateTime);

		if (StringUtils.isNotBlank(keyWord)) {
			queryWrapper.and(wrapper ->
					wrapper.like(XdPeople::getName, keyWord)
							.or().like(XdPeople::getMobile, AES.AESjiami(keyWord)))
							.or().like(XdPeople::getIdCard, AES.AESjiami(keyWord)

			);
		}

        if(isKey != null ){
            queryWrapper.eq(XdPeople::getIsKey, isKey);
        }
		IPage<XdPeople> xdPeoples = xdPeopleService.listXdPeople(page, queryWrapper);
		result.setSuccess(true);
		List<XdPeople> records = xdPeoples.getRecords();
		for (int i = 0; i < records.size(); i++) {
			XdPeople re =  records.get(i);
			re.setMobile(AES.AESjiemi(re.getMobile()));
			re.setIdCard(AES.AESjiemi(re.getIdCard()));
			re.setName(re.getName());
            re.setCreateTime(re.getCreateTime().replaceAll("-","/"));
			/*re.setMobile(AES.mobileIdCard(re.getMobile()));
			re.setIdCard(AES.mobileIdCard(re.getIdCard()));
			re.setName(AES.nameXx(re.getName()));*/
		}
		xdPeoples.setRecords(records);
		result.setResult(xdPeoples);
		return result;
	}

	/**
	 * 添加
	 *
	 * @param xdPeople
	 * @return
	 */
	@AutoLog(value = "西店数据-添加")
	@ApiOperation(value="西店数据-添加", notes="西店数据-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody XdPeople xdPeople) {
		xdPeopleService.save(xdPeople);
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param xdPeople
	 * @return
	 */
	@AutoLog(value = "西店数据-编辑")
	@ApiOperation(value="西店数据-编辑", notes="西店数据-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody XdPeople xdPeople) {
		xdPeopleService.updateById(xdPeople);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "西店数据-通过id删除")
	@ApiOperation(value="西店数据-通过id删除", notes="西店数据-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		xdPeopleService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "西店数据-批量删除")
	@ApiOperation(value="西店数据-批量删除", notes="西店数据-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.xdPeopleService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "西店数据-通过id查询")
	@ApiOperation(value="西店数据-通过id查询", notes="西店数据-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		XdPeople xdPeople = xdPeopleService.getById(id);
		return Result.ok(xdPeople);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param xdPeople
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, XdPeople xdPeople) {
      return super.exportXls(request, xdPeople, XdPeople.class, "西店数据");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, XdPeople.class);
  }

}
